package contracts

import (
	"math/big"
	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"github.com/ethereum/go-ethereum/common"
	"github.com/OpenBazaar/openbazaar-go/incubator/contracts/ABI"
	"github.com/OpenBazaar/openbazaar-go/incubator/datastructure"
	"fmt"
)

const TradeContractAddr  = "0x85b8bc1fe68841dc045b85f7443587e27f9ce67d"
const DataContractAddr   = "0x863f3fb7ee3baa292b2269483b4f10da32d2fa21"
//const OrderContractAddr  = "0x963bb1ca34f8002551a51d757b71a603c1334190"
//const OrderContractAddr  = "0x722fdc930e319cb392bfa5fafe04873a20d5a556"//test
const OrderContractAddr  = "0x2df925523bf0121333fa3068f90d1cf7497f87de"//139

const (
	Trade int = 0
	Data  int = 1
)

func CallOrderContract(contract int) (*Token, error){
	var orderToken *Token
	var error error
	//双合约暂时废弃
	//if contract == Trade {
	//	orderToken, error = CallContract(TradeContractAddr, ABI.TradeABI)
	//}else {
	//	orderToken, error = CallContract(DataContractAddr, ABI.DataABI)
	//}
	orderToken, error = CallContract(OrderContractAddr, ABI.OrderABI)
	return orderToken, error
}

func (_MyOrder *TokenTransactor)CreatOrder(opts *bind.TransactOpts, orderContract datastructure.OrderContract) (error) {
	tx, err := _MyOrder.contract.Transact(opts, "creatOrder", orderContract.Id, orderContract.BuyerId, orderContract.SellerId,
		orderContract.Buyer, orderContract.Seller, orderContract.TotalPrice, orderContract.GoodJson, orderContract.ShipJson, orderContract.OtherJson)
	if err != nil {
		fmt.Println(err.Error())
	}else {
		println("这是发送交易的回执" + tx.String())
	}
	return err
}

func (_MyOrder *TokenCaller) GetOrderById(opts *bind.CallOpts, id *big.Int) (datastructure.OrderContract, uint8, error) {
	var (
		_buyerId       = new(string)
		_sellerId      = new(string)
		_buyer         = new(common.Address)
		_seller        = new(common.Address)
		_totalPrice     = new(*big.Int)
		_goodJson      = new(string)
		_shipJson      = new(string)
		_state         = new(*big.Int)
		_otherJson     = new(string)
	)
	out := &[]interface{}{
		_buyerId,
		_sellerId,
		_buyer,
		_seller,
		_totalPrice,
		_goodJson,
		_shipJson,
		_state,
		_otherJson,
	}
	err := _MyOrder.contract.Call(opts, out, "getOrderById", id)
	ret := datastructure.OrderContract{
		id,
		*_buyerId,
		*_sellerId,
		*_buyer,
		*_seller,
		*_totalPrice,
		*_goodJson,
		*_shipJson,
		*_otherJson,
	}
	state := uint8((*_state).Int64())
	return ret, map_state[state], err
}

var map_state = map[uint8]uint8{
	0: 2,//已下单
	1: 5,//已发货
	2: 6,//已完成
	3: 8,//卖家拒绝订单
	4: 7,//买家取消订单
	8: 9,//退货
	9: 3,//卖家接受订单
}

func (_MyOrder *TokenCaller) GetIdsByBuyId(opts *bind.CallOpts, buyer string) ([]*big.Int, error) {
	var (
		ret0 = new([]*big.Int)
	)
	out := ret0
	err := _MyOrder.contract.Call(opts, out, "getBuyerIds", buyer)
	return *ret0, err
}

func (_MyOrder *TokenCaller) GetIdsBySellId(opts *bind.CallOpts, seller string) ([]*big.Int, error) {
	var (
		ret0 = new([]*big.Int)
	)
	out := ret0
	err := _MyOrder.contract.Call(opts, out, "getSellerIds", seller)
	return *ret0, err
}

func (_MyOrder *TokenTransactor) CancelOrder(opts *bind.TransactOpts, _id *big.Int, _otherJson string) (error) {
	tx, err := _MyOrder.contract.Transact(opts, "cancelOrderByBuyer", _id, _otherJson)
	if err != nil {
		fmt.Println(err.Error())
	}else {
		println("这是发送交易的回执" + tx.String())
	}
	return err
}

func (_MyOrder *TokenTransactor) AcceptOrder(opts *bind.TransactOpts, _id *big.Int, _otherJson string) (error) {
	tx, err := _MyOrder.contract.Transact(opts, "sellerConfirm", _id, _otherJson)
	if err != nil {
		fmt.Println(err.Error())
	}else {
		println("这是发送交易的回执" + tx.String())
	}
	return err
}

func (_MyOrder *TokenTransactor) RejectOrder(opts *bind.TransactOpts, _id *big.Int, _otherJson string) (error) {
	tx, err := _MyOrder.contract.Transact(opts, "cancelOrder", _id, _otherJson)
	if err != nil {
		fmt.Println(err.Error())
	}else {
		println("这是发送交易的回执" + tx.String())
	}
	return err
}

func (_MyOrder *TokenTransactor) DeliveryConfirm(opts *bind.TransactOpts, _id *big.Int, _shipJson string, _otherJson string) (error) {
	tx, err := _MyOrder.contract.Transact(opts, "deliveryConfirm", _id, _shipJson, _otherJson)
	if err != nil {
		fmt.Println(err.Error())
	}else {
		println("这是发送交易的回执" + tx.String())
	}
	return err
}

func (_MyOrder *TokenTransactor) ReceivedConfirm(opts *bind.TransactOpts, _id *big.Int, _otherJson string) (error) {
	fmt.Println(_id,_otherJson)
	tx, err := _MyOrder.contract.Transact(opts, "receivedConfirm", _id, _otherJson)
	if err != nil {
		fmt.Println(err.Error())
	}else {
		println("这是发送交易的回执" + tx.String())
	}
	return err
}